﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZY.EntityFrameWork.Core.Model.Entity.Authority;

namespace ZY.EntityFrameWork.Core.Services
{
    public abstract partial class BaseAuthorityService
    {
        public List<Role> GetAllRoles()
        {
            return roleRepository.GetQueryable().ToList();
        }

        public List<RoleModule> GetRolePermissions(string roleName)
        {
            Role role=roleRepository.FindSingle(q=>q.RoleName==roleName);
            return role == null ? null : role.RoleModules.ToList();
        }

        /// <summary>
        /// 更新用户权限
        /// </summary>
        /// <param name="roleModules"></param>
        /// <returns></returns>
        public int UpdateRole(List<RoleModule> roleModules)
        {
            foreach (RoleModule roleModule in roleModules)
            {
                RoleModule old = roleModuleRepository.FindSingle(q =>
                    q.ModuleId == roleModule.ModuleId && q.RoleId == roleModule.RoleId);
                if (old == null)
                {
                    roleModuleRepository.Insert(roleModule,false);//新增权限
                }
                else
                {
                    old.Permissions=roleModule.Permissions;
                    roleModuleRepository.Update(old,false);//修改已存在权限
                }
            }
            return Context.Commit();
        }
    }
}
